package com.singlemountain.gcommerce.web.account;

import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.web.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: sunny
 * Date: 12-2-17
 * Time: 下午4:13
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class MyAccountUserEditController extends BaseController {
    public final String URL_MY_ACCOUNT_USER_EDIT = "myAccountUserEdit";
    public final String URL_MY_ACCOUNT_USER_EDIT_PASSWORD = "myAccountUserEditPassword";
    public final String VIEW_MY_ACCOUNT_USER_EDIT = "view_myAccountUserEdit";
    boolean isEdit = false;
    boolean isEditPassword=false;

    //初始化userd的对象
    @ModelAttribute
    public Userd setModel(ModelMap modelMap, HttpSession userSession) {
        return getLoginUser(userSession);
    }

    //点击登录按钮后显示登录页面
    @RequestMapping(value = URL_MY_ACCOUNT_USER_EDIT, method = RequestMethod.GET)
    public String SetUp(ModelMap modelMap) {
        isEdit = false;
        boolean isEditPassword=false;
        modelMap.addAttribute("isEdit", isEdit);
        modelMap.addAttribute("isEditPassword", isEditPassword);
        return VIEW_MY_ACCOUNT_USER_EDIT;
    }


    //执行修改信息
    @RequestMapping(value = URL_MY_ACCOUNT_USER_EDIT, method = RequestMethod.POST)
    public String doUserEdit(ModelMap modelMap,Userd userd, HttpSession userSession) {
        userdService.saveUserd(userd);
        isEdit = true;
        boolean isEditPassword=false;
        modelMap.addAttribute("isEdit", isEdit);
        modelMap.addAttribute("isEditPassword", isEditPassword);
        return VIEW_MY_ACCOUNT_USER_EDIT;
    }

    @RequestMapping(value = URL_MY_ACCOUNT_USER_EDIT_PASSWORD, method = RequestMethod.POST)
    public String doPasswordEdit(HttpServletRequest request,ModelMap modelMap,Userd userd, HttpSession userSession) {

        isEditPassword = false;
        modelMap.addAttribute("isEdit", isEdit);

        if(getLoginUser(userSession).getPassword().equals(request.getParameter("oldPassword"))){
            userdService.saveUserd(userd);
            boolean isEditPassword=true;
            modelMap.addAttribute("isEditPassword", isEditPassword);
        }else{

        }

        return VIEW_MY_ACCOUNT_USER_EDIT;
    }


}
